#include "socket_deal_with.h"

void *socket_start(void){
	int socket_fd;
	socket_fd = socket(AF_INET, SOCK_STREAM, 0);
	if(socket_fd == -1){
		perror("create socket fail! \n");
	}

	struct sockaddr_in my_addr;
	my_addr.sin_family = AF_INET;
	my_addr.sin_addr.s_addr = htons(INADDR_ANY);
	my_addr.sin_port = htons(SERVERPORT);
	bzero(&(my_addr.sin_zero), 8);
	if(bind(socket_fd, (struct sockaddr*)&my_addr, sizeof(my_addr)) == -1){
		perror("bind socket fail! \n");
	}
	if(listen(socket_fd, BACKLOG) == -1){
		perror("listen socket fail! \n");
	}

	int sin_size;
	struct sockaddr_in remote_addr;
	int client_fd;
	sin_size = sizeof(struct sockaddr_in);
	while(1){
		if((client_fd = accept(socket_fd, (struct sockaddr*)&remote_addr, &sin_size)) == -1){
			perror("accept socket fail! \n");
			continue;
		}
		char buf[MAXBUFSIZE];
		int n = recv(client_fd, buf, MAXBUFSIZE, 0);
		if(n < 0){
			perror("recv error!\n");
			continue;
		}else{
			buf[n] = '\0';
			printf("recv message from client :%s, n:%d\n", buf, n);
			deal_with_recv(buf, client_fd);
		}
		close(client_fd);
	}
}
int pthread_socket_start(void){
	pthread_t tid;
	int ret = 0;
	ret = pthread_create(&tid, NULL, (void *)socket_start, NULL);
	if(ret != 0){
		printf("create pthread fail!\n");
	}
	return ret;
}

void *deal_with_recv(char *buf, int client_fd){
	char *splitStr = strchr(buf, '$');
	printf("splitStr :%s\n", splitStr);
	switch(buf[0]){
		case '1':
			printf("some one login!\n");
			check_account(splitStr + 1, client_fd);
			break;
		case '2':
			printf("some one want to talk with anther\n");
			break;
		case '3':
			printf("dealing with getting request\n");
			getRequest(splitStr + 1, client_fd);
			break;
		case '4':
			printf("dealing with setting request\n");
			setRequest(splitStr + 1, client_fd);
			break;
		default:
			break;
	}
	
}

void check_account(char *buf, int client_fd){
	char *splitStrId = strchr(buf, ':');
	char *splitStr = strchr(splitStrId, '$');
	char *splitStrPassword = strchr(splitStr, ':');
	int idLen = splitStr - splitStrId;
	int passwordLen = strlen(splitStrPassword);
	
	char *id, *password;
	id = (char *)malloc(idLen+1);
	password = (char *)malloc(passwordLen+1);
	if(id == NULL || password == NULL)
		goto err;
	memset(id, 0, idLen);
	memset(password, 0, passwordLen);
	strncpy(id, splitStrId+1, idLen-1);
	strncpy(password, splitStrPassword+1, passwordLen-1);

	char sqlStr[MAXBUFSIZE] = "select * from user where id=";
	char retStr[MAXBUFSIZE];
	memset(retStr, '0', MAXBUFSIZE);
	strcat(sqlStr, id);
	strcat(sqlStr, " and password=");
	strcat(sqlStr, password);
	if(mysql_select_result(sqlStr, retStr)){
		printf("login success \n ");
		printf("%s, %d\n", retStr, sizeof(retStr));
		if(send(client_fd, "ok", 10, 0) < 0){
			printf("send err!\n");
		}
	}else{
		printf("illega loginning!\n ");
		if(send(client_fd, "err", 10, 0) < 0){
			printf("send err! \n");
		}
	}
err:
	if(id != NULL)
		free(id);
	if(password != NULL)
		free(password);
}

void getRequest(char *buf, int client_fd){
	char *splitId = strchr(buf, ':');
	char *splitStr = strchr(buf, '$');
	int idLen = splitStr - splitId;
	char id[idLen];
	memset(id, 0, idLen);
	strncpy(id, splitId + 1, idLen - 1);
	char sqlStr[MAXBUFSIZE] = "select name from user where id=";
	strcat(sqlStr, id);
	char retStr[MAXBUFSIZE];
	memset(retStr, 0, MAXBUFSIZE);
	if(mysql_select_result(sqlStr, retStr)){
		printf("get result\n");	
		printf("%s, %s\n", id, retStr);
		if(send(client_fd, retStr, sizeof(retStr), 0) < 0){
			printf("send err!\n");
		}
	}else{
		printf("can't get result");
	}
}

void setRequest(char *buf, int client_fd){
}
